/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "TestResultAnalysisPlot.h"
#include "Options.h"

TestResultAnalysisPlot::TestResultAnalysisPlot(QDialog* parent):
	QDialog(parent)
{
	ui.setupUi(this);

	QObject::connect(ui.closeButton, SIGNAL(clicked()), this, SLOT(accept()));

	ui.TestResultAnalysisPlotTab->clear();
	QWidget* testDateWidget = new QWidget(this);
	QGridLayout* testDateGrid = new QGridLayout(testDateWidget);
	testDateGrid->addWidget(&mTestNewDatePlot, 0, 0);
	testDateGrid->addWidget(&mReviewDatePlot, 1, 0);
	testDateGrid->setSpacing(0);

	QWidget* diffWidget = new QWidget(this);
	QScrollArea* diffLevelScrollArea = new QScrollArea();
	diffLevelScrollArea->setWidget(diffWidget);
	diffLevelScrollArea->setWidgetResizable(true);

	ui.TestResultAnalysisPlotTab->addTab(testDateWidget, tr("Test Date"));
	ui.TestResultAnalysisPlotTab->addTab(&mReciteProgressPlot, tr("Test Progress"));

	this->accept();
}

TestResultAnalysisPlot::~TestResultAnalysisPlot()
{
}

void TestResultAnalysisPlot::drawTestDateGraph(map<QDate, int>& reviewDateCount, map<QDate, int>& reciteNewDateCount)
{
	mTestNewDatePlot.clear();
	mReviewDatePlot.clear();

	//plot reciteNewDateCount
	map<QDate, int>::iterator count_it;
	QDate today = OPT.getCurrendDate();
	QwtArray<QwtDoubleInterval> intervals;
	QwtArray<double> values;

	mTestNewDatePlot.setCanvasBackground(QColor(Qt::white));
	mTestNewDatePlot.setTitle(tr("Number of new words tested every day."));

	QwtPlotGrid *reciteNewDatePlotGrid = new QwtPlotGrid;
	reciteNewDatePlotGrid->enableXMin(true);
	reciteNewDatePlotGrid->enableYMin(true);
	reciteNewDatePlotGrid->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
	reciteNewDatePlotGrid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
	reciteNewDatePlotGrid->attach(&mTestNewDatePlot);

	HistogramItem *reciteNewDateHistogram = new HistogramItem();
	reciteNewDateHistogram->setColor(Qt::darkCyan);

	intervals.clear();
	values.clear();

	for (count_it = reciteNewDateCount.begin(); count_it != reciteNewDateCount.end(); count_it++)
	{
		//LOG<<count_it->first.toString(DATE_FORMAT).toUtf8().data()<<": "<<count_it->second<<endl;
		int interval = today.daysTo(count_it->first);
		int count = count_it->second;

		float width = 0.8f;
		intervals.push_back(QwtDoubleInterval(interval-width/2, interval+width/2));
		values.push_back(count);

	}

	reciteNewDateHistogram->setData(QwtIntervalData(intervals, values));
	reciteNewDateHistogram->attach(&mTestNewDatePlot);

	mTestNewDatePlot.setAxisAutoScale(QwtPlot::yLeft);
	mTestNewDatePlot.setAxisAutoScale(QwtPlot::xBottom);
	mTestNewDatePlot.replot();

	//plot reviewDateCount
	mReviewDatePlot.setCanvasBackground(QColor(Qt::white));
	mReviewDatePlot.setTitle(tr("Number of words reviewed every day"));

	QwtPlotGrid *reviewDatePlotGrid = new QwtPlotGrid;
	reviewDatePlotGrid->enableXMin(true);
	reviewDatePlotGrid->enableYMin(true);
	reviewDatePlotGrid->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
	reviewDatePlotGrid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
	reviewDatePlotGrid->attach(&mReviewDatePlot);

	HistogramItem *reviewDateHistogram = new HistogramItem();
	reviewDateHistogram->setColor(Qt::darkCyan);

	intervals.clear();
	values.clear();

	for (count_it = reviewDateCount.begin(); count_it != reviewDateCount.end(); count_it++)
	{
		//LOG<<count_it->first.toString(DATE_FORMAT).toUtf8().data()<<": "<<count_it->second<<endl;
		int interval = today.daysTo(count_it->first);
		int count = count_it->second;

		float width = 0.8f;
		intervals.push_back(QwtDoubleInterval(interval-width/2, interval+width/2));
		values.push_back(count);
	}

	reviewDateHistogram->setData(QwtIntervalData(intervals, values));
	reviewDateHistogram->attach(&mReviewDatePlot);

	mReviewDatePlot.setAxisAutoScale(QwtPlot::yLeft);
	mReviewDatePlot.setAxisAutoScale(QwtPlot::xBottom);
	mReviewDatePlot.replot();
}

void TestResultAnalysisPlot::drawTestProgress(map<int, int>& reciteProgress)
{
	mReciteProgressPlot.clear();

	map<int, int>::iterator count_it;
	QwtArray<QwtDoubleInterval> intervals;
	QwtArray<double> values;

	mReciteProgressPlot.setCanvasBackground(QColor(Qt::white));
	mReciteProgressPlot.setTitle(tr("Test Progress"));

	QwtPlotGrid *grid = new QwtPlotGrid;
	grid->enableXMin(true);
	grid->enableYMin(true);
	grid->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
	grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
	grid->attach(&mReciteProgressPlot);

	HistogramItem *histogram = new HistogramItem();
	histogram->setColor(Qt::darkCyan);

	intervals.clear();
	values.clear();

	for (count_it = reciteProgress.begin(); count_it != reciteProgress.end(); count_it++)
	{
		int interval =count_it->first;
		int count = count_it->second;

		float width = 0.8f;
		intervals.push_back(QwtDoubleInterval(interval-width/2, interval+width/2));
		values.push_back(count);

	}

	histogram->setData(QwtIntervalData(intervals, values));
	histogram->attach(&mReciteProgressPlot);

	mReciteProgressPlot.setAxisAutoScale(QwtPlot::yLeft);
	mReciteProgressPlot.setAxisTitle(QwtPlot::yLeft, tr("Word Count"));
	mReciteProgressPlot.setAxisAutoScale(QwtPlot::xBottom);
	mReciteProgressPlot.setAxisTitle(QwtPlot::xBottom, tr("Test Count"));

	mReciteProgressPlot.replot();
}
